
#include "ws2812_drv.h"
#include "rgb_spi_drv.h"
#include "my_gpio.h"


typedef enum{
	ICON_NULL = 0,
	ICON_K1 = 1,
	ICON_K2,

	ICON_B1,
	ICON_C1,

	ICON_A2,
	ICON_B2,
	ICON_C2,
	ICON_D2,
	ICON_E2,
	ICON_F2,
	ICON_G2,

	ICON_A3,
	ICON_B3,
	ICON_C3,
	ICON_D3,
	ICON_E3,
	ICON_F3,
	ICON_G3,
}ICON_CODE_e;


typedef enum{
	// 个位
	GE_WEI_0 = 0,
	GE_WEI_1,
	GE_WEI_2,
	GE_WEI_3,
	GE_WEI_4,
	GE_WEI_5,
	GE_WEI_6,

	GE_WEI_7,
	GE_WEI_8,
	GE_WEI_9,
	// 十位
	SHI_WEI_0,
	SHI_WEI_1,
	SHI_WEI_2,
	SHI_WEI_3,

	SHI_WEI_4,
	SHI_WEI_5,
	SHI_WEI_6,
	SHI_WEI_7,
	SHI_WEI_8,
	SHI_WEI_9,
	// 百位
	BAI_WEI_1,
}DISP_NUMBER_e;


typedef struct 
{
	/* data */
	DISP_NUMBER_e number;
	ICON_CODE_e icon[8];
}DISP_NUM_t;

DISP_NUM_t disp_num[3];

uint8_t seg1_data = 0x00;
uint8_t seg2_data = 0x00;
uint8_t seg3_data = 0x00;
uint8_t seg4_data = 0x00;
uint8_t seg5_data = 0x00;


/******************************************/
//延时 函数
static void I2C_delay(void)
{
	u8 t=10;
	while(t--);
}

/*************************************
 * IIC DATA
 *************************************/ 
__INLINE void my_soft_data_high(void)
{
	set_pin_high(SOFT_IIC_DATA_GPIO, SOFT_IIC_DATA_PIN);
}

/*************************************
 * IIC DATA
 *************************************/ 
__INLINE void my_soft_data_low(void)
{
	set_pin_low(SOFT_IIC_DATA_GPIO, SOFT_IIC_DATA_PIN);
}


/**********************************************
 * IIC CLK
 **********************************************/ 
__INLINE void my_soft_sck_high(void)
{
	set_pin_high(SOFT_IIC_SCL_GPIO, SOFT_IIC_SCL_PIN);
}

__INLINE void my_soft_sck_low(void)
{
	set_pin_low(SOFT_IIC_SCL_GPIO, SOFT_IIC_SCL_PIN);
}

//========================================================================
// 函数: void iic_start (void)
// 描述: 
// 参数: 启始信号
// 返回: none.
//========================================================================
static void iic_start(void)
{
	gpio_output_cfg(SOFT_IIC_DATA_GPIO, SOFT_IIC_DATA_PIN);
	I2C_delay();
	my_soft_data_high();
	I2C_delay();
	my_soft_sck_high();
	I2C_delay();
	my_soft_data_low();
	I2C_delay();
	I2C_delay();
	I2C_delay();
	my_soft_sck_low();
}

//========================================================================
// 函数: void iic_stop (void)
// 描述: 
// 参数: 启始信号
// 返回: none.
//========================================================================
static void iic_stop(void)
{
	gpio_output_cfg(SOFT_IIC_DATA_GPIO, SOFT_IIC_DATA_PIN);

	I2C_delay();
	my_soft_sck_high();
	I2C_delay();
	my_soft_data_low();
	I2C_delay();
	I2C_delay();
	my_soft_data_high();
	I2C_delay();
	// my_soft_sck_low();
    I2C_delay();
    my_soft_sck_high();
}


//========================================================================
// 函数: void SendData (char dat)
// 描述: 
// 参数: 发送的数据. ****发送8bit数据，从低位开始***
// 返回: none.
//========================================================================
static void iic_send_one_byte(u8 data)
{
	u8 i;
    gpio_output_cfg(SOFT_IIC_DATA_GPIO, SOFT_IIC_DATA_PIN);

	for(i=0;i<8; i++)
	{
		my_soft_sck_low();//将时钟信号设置为低电平
        //将dat的8位从 从低位开始
		if(data&0x01){
			my_soft_data_high();
        }
		else{
			my_soft_data_low();
        }
		I2C_delay();
		my_soft_sck_high();
	
		data >>=1;
        I2C_delay();
        I2C_delay();
    }
}

//========================================================================
// 函数:  
// 描述:  显示字符，十位 显示字符“0”
// 参数: 
// 返回: none.
//========================================================================
__INLINE void ge_wei_num0(void)
{
	iic_start();
	iic_send_one_byte(0xc0);	//地址命令设置：初始地址00H  对应A0行
	iic_send_one_byte(0x00);
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc1);	//地址命令设置：初始地址00H 对应A1行
	iic_send_one_byte(0x0e);		
    iic_stop();


    iic_start();
    iic_send_one_byte(0xc2);	//地址命令设置：初始地址00H 对应A2行
	iic_send_one_byte(0x02);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc3);	//地址命令设置：初始地址00H 对应A3行
	iic_send_one_byte(0x02);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc4);	//地址命令设置：初始地址00H 对应A4行
	iic_send_one_byte(0x02);		
    iic_stop();

   	iic_start();
	iic_send_one_byte(0xc5);	//地址命令设置：初始地址00H 对应A5行
	iic_send_one_byte(0x02);
    iic_stop();

}

//========================================================================
// 函数:  
// 描述:  显示字符，十位 显示字符“0”
// 参数:  对应断码字段“A3, B3, C3, D3,E3, F3”
// 返回: none.
//========================================================================
 void disp_num0(uint8_t blot_icon_flag)
{
	iic_start();
	iic_send_one_byte(0xc0);	//地址命令设置：初始地址00H  对应A0行
	iic_send_one_byte(0x00);
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc1);	//地址命令设置：初始地址00H 对应A1行
	iic_send_one_byte(0x0e);		
    iic_stop();


    iic_start();
    iic_send_one_byte(0xc2);	//地址命令设置：初始地址00H 对应A2行
	iic_send_one_byte(0x12);	
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc3);	//地址命令设置：初始地址00H 对应A3行
	if(blot_icon_flag){
		iic_send_one_byte(0x12);
	}
	else{
		iic_send_one_byte(0x02);
	}		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc4);	//地址命令设置：初始地址00H 对应A4行
	iic_send_one_byte(0x02);		
    iic_stop();

   	iic_start();
	iic_send_one_byte(0xc5);	//地址命令设置：初始地址00H 对应A5行
	iic_send_one_byte(0x00);
    iic_stop();
}


//========================================================================
// 函数:  
// 描述:  显示字符，十位 显示字符“1”
// 参数: 		 对应断码字段“ B3, C3,""
// 返回: none.
//========================================================================
 void disp_num1(uint8_t blot_icon_flag)
{
	iic_start();
	iic_send_one_byte(0xc0);	//地址命令设置：初始地址00H  对应A0行
	iic_send_one_byte(0x00);
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc1);	//地址命令设置：初始地址00H 对应A1行
	iic_send_one_byte(0x04);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc2);	//地址命令设置：初始地址00H 对应A2行
	iic_send_one_byte(0x12);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc3);	//地址命令设置：初始地址00H 对应A3行
	if(blot_icon_flag){
		iic_send_one_byte(0x10);
	}
	else{
		iic_send_one_byte(0x00);
	}	
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc4);	//地址命令设置：初始地址00H 对应A4行
	iic_send_one_byte(0x00);		
    iic_stop();

   	iic_start();
	iic_send_one_byte(0xc5);	//地址命令设置：初始地址00H 对应A5行
	iic_send_one_byte(0x00);
    iic_stop();

}


//========================================================================
// 函数:  
// 描述: icon 对应的 段
// 参数: 		 
// 返回: none.
//========================================================================

static void icon_to_seg(ICON_CODE_e icon)
{
	switch(icon){
		case ICON_K1:{
			seg3_data |= 0x10;
			break;
		}
		case ICON_K2:{
			seg2_data |= 0x10;
			break;
		}
		case ICON_B1:{
			seg3_data |= 0x08;
			break;
		}
		case ICON_C1:{
			seg2_data |= 0x08;
			break;
		}
		case ICON_A2:{
			seg2_data |= 0x04;
			break;
		}
		case ICON_B2:{
			seg3_data |= 0x04;
			break;
		}
		case ICON_C2:{
			seg4_data |= 0x08;
			break;
		}
		case ICON_D2:{
			seg4_data |= 0x04;
			break;
		}
		case ICON_E2:{
			seg5_data |= 0x04;
			break;
		}
		case ICON_F2:{
			seg5_data |= 0x08;
			break;
		}
		case ICON_G2:{
			seg5_data |= 0x10;
			break;
		}
		case ICON_A3:{
			seg1_data |= 0x02;
			break;
		}
		case ICON_B3:{
			seg2_data |= 0x02;
			break;
		}
		case ICON_C3:{
			seg1_data |= 0x04;
			break;
		}
		case ICON_D3:{
			seg3_data |= 0x02;
			break;
		}
		case ICON_E3:{
			seg1_data |= 0x08;
			break;
		}
		case ICON_F3:{
			seg4_data |= 0x02;
			break;
		}
		case ICON_G3:{
			seg5_data |= 0x02;
			break;
		}
		default:{
			break;
		}
	}
}

//========================================================================
// 函数:  
// 描述: number 对应的 段
// 参数: 		 
// 返回: none.
//========================================================================

static void number_to_icon(DISP_NUMBER_e num)
{
	
	switch(num){
		// 个位
		case GE_WEI_0:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_D3;
			disp_num[0].icon[4] = ICON_E3;
			disp_num[0].icon[5] = ICON_F3;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}

		case GE_WEI_1:{
			disp_num[0].icon[0] = ICON_B3;
			disp_num[0].icon[1] = ICON_C3;
			disp_num[0].icon[2] = ICON_NULL;
			disp_num[0].icon[3] = ICON_NULL;
			disp_num[0].icon[4] = ICON_NULL;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_2:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_E3;
			disp_num[0].icon[3] = ICON_G3;
			disp_num[0].icon[4] = ICON_D3;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_3:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_G3;
			disp_num[0].icon[4] = ICON_D3;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_4:{
			disp_num[0].icon[0] = ICON_F3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_G3;
			disp_num[0].icon[4] = ICON_NULL;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}

		case GE_WEI_5:{
			disp_num[0].icon[0] = ICON_F3;
			disp_num[0].icon[1] = ICON_A3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_G3;
			disp_num[0].icon[4] = ICON_D3;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_6:{
			disp_num[0].icon[0] = ICON_F3;
			disp_num[0].icon[1] = ICON_A3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_G3;
			disp_num[0].icon[4] = ICON_D3;
			disp_num[0].icon[5] = ICON_E3;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_7:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_NULL;
			disp_num[0].icon[4] = ICON_NULL;
			disp_num[0].icon[5] = ICON_NULL;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}

		case GE_WEI_8:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_D3;
			disp_num[0].icon[4] = ICON_E3;
			disp_num[0].icon[5] = ICON_F3;
			disp_num[0].icon[6] = ICON_G3;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		case GE_WEI_9:{
			disp_num[0].icon[0] = ICON_A3;
			disp_num[0].icon[1] = ICON_B3;
			disp_num[0].icon[2] = ICON_C3;
			disp_num[0].icon[3] = ICON_D3;
			disp_num[0].icon[4] = ICON_G3;
			disp_num[0].icon[5] = ICON_F3;
			disp_num[0].icon[6] = ICON_NULL;
			disp_num[0].icon[7] = ICON_NULL;
			break;
		}
		// 十位----------------
		case SHI_WEI_0:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_D2;
			disp_num[1].icon[4] = ICON_E2;
			disp_num[1].icon[5] = ICON_F2;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		
		case SHI_WEI_1:{
			disp_num[1].icon[0] = ICON_B2;
			disp_num[1].icon[1] = ICON_C2;
			disp_num[1].icon[2] = ICON_NULL;
			disp_num[1].icon[3] = ICON_NULL;
			disp_num[1].icon[4] = ICON_NULL;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_2:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_E2;
			disp_num[1].icon[3] = ICON_G2;
			disp_num[1].icon[4] = ICON_D2;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_3:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_G2;
			disp_num[1].icon[4] = ICON_D2;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_4:{
			disp_num[1].icon[0] = ICON_F2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_G2;
			disp_num[1].icon[4] = ICON_NULL;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_5:{
			disp_num[1].icon[0] = ICON_F2;
			disp_num[1].icon[1] = ICON_A2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_G2;
			disp_num[1].icon[4] = ICON_D2;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_6:{
			disp_num[1].icon[0] = ICON_F2;
			disp_num[1].icon[1] = ICON_A2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_G2;
			disp_num[1].icon[4] = ICON_D2;
			disp_num[1].icon[5] = ICON_E2;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_7:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_NULL;
			disp_num[1].icon[4] = ICON_NULL;
			disp_num[1].icon[5] = ICON_NULL;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_8:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_D2;
			disp_num[1].icon[4] = ICON_E2;
			disp_num[1].icon[5] = ICON_F2;
			disp_num[1].icon[6] = ICON_G2;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		case SHI_WEI_9:{
			disp_num[1].icon[0] = ICON_A2;
			disp_num[1].icon[1] = ICON_B2;
			disp_num[1].icon[2] = ICON_C2;
			disp_num[1].icon[3] = ICON_D2;
			disp_num[1].icon[4] = ICON_G2;
			disp_num[1].icon[5] = ICON_F2;
			disp_num[1].icon[6] = ICON_NULL;
			disp_num[1].icon[7] = ICON_NULL;
			break;
		}
		//---------百位
		case BAI_WEI_1:{
			disp_num[2].icon[0] = ICON_B1;
			disp_num[2].icon[1] = ICON_C1;
			disp_num[2].icon[2] = ICON_NULL;
			disp_num[2].icon[3] = ICON_NULL;
			disp_num[2].icon[4] = ICON_NULL;
			disp_num[2].icon[5] = ICON_NULL;
			disp_num[2].icon[6] = ICON_NULL;
			disp_num[2].icon[7] = ICON_NULL;
			break;
		}	
		default:{

			break;
		}

	}



}






//========================================================================
// 函数:  
// 描述:  显示字符，十位 显示字符“2”
// 参数: 
// 返回: none.
//========================================================================
__INLINE void total_seg_disp(void)
{
	iic_start();
    iic_send_one_byte(0xc0);	//地址命令设置：初始地址00H，对应A1行
	iic_send_one_byte(0);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc1);	//地址命令设置：初始地址00H，对应A1行
	iic_send_one_byte(seg1_data);		
    iic_stop();


    iic_start();
    iic_send_one_byte(0xc2);	//地址命令设置：初始地址00H 对应A2行
	iic_send_one_byte(seg2_data);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc3);	//地址命令设置：初始地址00H  对应A3行
	iic_send_one_byte(seg3_data);		
    iic_stop();

    iic_start();
    iic_send_one_byte(0xc4);	//地址命令设置：初始地址00H 对应A4行
	iic_send_one_byte(seg4_data);		
    iic_stop();

   	iic_start();
	iic_send_one_byte(0xc5);	//地址命令设置：初始地址00H 对应A5行
	iic_send_one_byte(seg5_data);
    iic_stop();
}


//========================================================================
// 函数: void SendData (char dat)
// 描述: 
// 参数: 发送的数据. ****发送8bit数据，从低位开始***
// 返回: none.
//========================================================================
/************显示函数，数码管1~16位显示0~F************/
void disp_test(void)
{
    unsigned char i;
	static int disp_cnt;



	seg1_data = 0x00;
	seg2_data = 0x00;
	seg3_data = 0x00;
	seg4_data = 0x00;
	seg5_data = 0x00;
	memset(disp_num, 0 ,3*sizeof(DISP_NUM_t));

	disp_cnt ++;
	
	disp_cnt = disp_cnt%121;

	// 个位
	number_to_icon(disp_cnt%10); 
	for(i= 0; i < 8; i ++)
	{
		if(disp_num[0].icon[i] == ICON_NULL){
			break;
		}
		icon_to_seg(disp_num[0].icon[i]);	
	}
	// 十位
	if(disp_cnt >= 10){
		number_to_icon((disp_cnt/10)%10 + SHI_WEI_0);
		for(i= 0; i < 8; i ++)
		{
			if(disp_num[1].icon[i] == ICON_NULL){
				break;
			}
			icon_to_seg(disp_num[1].icon[i]);	
		}
	}

	// 百位
	if(disp_cnt >= 100){
		number_to_icon(BAI_WEI_1);
		for(i= 0; i < 8; i ++)
		{
			if(disp_num[2].icon[i] == ICON_NULL){
				break;
			}
			icon_to_seg(disp_num[2].icon[i]);	
		}
	}
	// 闪电图标
	if(disp_cnt%2){
		icon_to_seg(ICON_K1);
	}
	
	// 百分比图标
	icon_to_seg(ICON_K2);
	total_seg_disp();

	// 打开显示
    iic_start();
    iic_send_one_byte(0x8c);	//显示控制：显示开，脉冲宽度设为11/16
    iic_stop();
}



/*******************************************
 * IIC CLK ,soft iic 初始化
 *******************************************/ 
void my_seg_led_init(void)
{
    gpio_output_cfg(SOFT_IIC_DATA_GPIO, SOFT_IIC_DATA_PIN);
    gpio_output_cfg(SOFT_IIC_SCL_GPIO, SOFT_IIC_SCL_PIN);
}


